package com.sctvcloud.qingchuan.ui.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.flyco.tablayout.SlidingTabLayout;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.base.BaseActivity;
import com.sctvcloud.qingchuan.beans.FocusColumn;
import com.sctvcloud.qingchuan.ui.adapter.PoliticsFormPageAdapter;
import com.sctvcloud.qingchuan.ui.utils.GlideCircleTransform;
import com.sctvcloud.qingchuan.ui.utils.GlideUtil;
import com.sctvcloud.qingchuan.utils.UserManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的问政主页
 */
public class MyPoliticsActivity extends BaseActivity {

    @BindView(R.id.item_user_avatar)
    protected CustomEXImageView avatar;
    @BindView(R.id.item_username)
    protected CustomFontTextView userName;

    @BindView(R.id.home_tablay)
    protected SlidingTabLayout home_tablay;
    @BindView(R.id.home_content_vp)
    protected ViewPager home_content_vp;

    @BindView(R.id.home_top_lay_tab)
    protected ViewGroup tabLay;
    @BindView(R.id.home_content_ll)
    protected LinearLayout content_ll;
    private PoliticsFormPageAdapter homePagerAdapter;
    private ArrayList<FocusColumn> list = new ArrayList<FocusColumn>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setThisImmersive(true);
        super.onCreate(savedInstanceState);
        initUser();
        initView();
        initData();
    }


    @Override
    protected void initInject() {
        setContentView(R.layout.activity_my_politics);
        ButterKnife.bind(this);
    }


    private void initView() {


    }

    private void initUser() {
        if (UserManager.getInstance().getUser() == null) {
            return;
        }
        userName.setText(UserManager.getInstance().getUser().getNickName());
        String icon = UserManager.getInstance().getUser().getAvatar();
        if (!TextUtils.isEmpty(icon)) {
            GlideUtil.getDynamicGlid(MyPoliticsActivity.this, icon, true).transform(new GlideCircleTransform(MyPoliticsActivity.this))
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(avatar);
        }

    }


    private void initData() {
        FocusColumn hots = new FocusColumn();
        hots.setChannelName("我的回音壁");
        hots.setChannelType(String.valueOf(PoliticsFormPageAdapter.TYPE_MINE_POLITICS));
        list.add(hots);
        setData(list);
    }

    private void setAdapterTopHeiht(boolean isNewInstance) {
        if (isNewInstance) {
            homePagerAdapter.setTopHeight(tabLay.getHeight());
        }
    }

    /**
     * 加载 首页中的各个栏目页面
     *
     * @param fChannels
     */
    private void setData(List<FocusColumn> fChannels) {
        if (ListUtils.isListValued(fChannels)) {
            content_ll.setVisibility(View.VISIBLE);
            boolean b = true;
            int s = -1;
            if (homePagerAdapter == null) {
                homePagerAdapter = new PoliticsFormPageAdapter(getSupportFragmentManager(), list);
                setAdapterTopHeiht(true);
                home_content_vp.setAdapter(homePagerAdapter);
                home_content_vp.setOffscreenPageLimit(1);
                home_content_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageSelected(int position) {
//                        if(position==3){
//                            if (!UserManager.isLoginS()) {
//                                toLogin();
//                            }
//                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                home_tablay.setViewPager(home_content_vp);
                s = homePagerAdapter.getCount();
            } else {
                s = homePagerAdapter.getCount();
                setAdapterTopHeiht(true);
                homePagerAdapter.setData(list);
                b = s != homePagerAdapter.getCount();
            }
//            home_tablay.setOnTabSelectListener(this);
//            refreshColumnTab(b);

        } else {
            content_ll.setVisibility(View.GONE);
        }


        //    home_tablay.setChangeSize(true);
        home_tablay.notifyDataSetChanged();
        home_tablay.setTabPadding(0);
        home_tablay.setTabSpaceEqual(true);
    }


    @OnClick(value = {R.id.title_top_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_top_back:
                finish();
                break;
        }
    }
}
